package cn.xinyangtong.shopping.rest.service;

import cn.xinyangtong.common.pojo.DefaultResult;
import cn.xinyangtong.common.util.ConstantsUtil;
import cn.xinyangtong.facade.pojo.Item;
import cn.xinyangtong.facade.rest.ItemService;
import cn.xinyangtong.mapper.TbItemDescMapper;
import cn.xinyangtong.mapper.TbItemMapper;
import cn.xinyangtong.mapper.TbItemParamItemMapper;
import cn.xinyangtong.pojo.*;
import cn.xinyangtong.shopping.rest.dao.JedisClient;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by fengbo on 2017/7/18.
 */
public class ItemServiceImpl implements ItemService {

    private final Logger log = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private TbItemParamItemMapper itemParamItemMapper;
    @Autowired
    private TbItemDescMapper itemDescMapper;
    @Autowired
    private JedisClient jedisClient;

    @Override
    public Item getItemBaseInfo(Long itemId) {
        final String redisKey = ConstantsUtil.REDIS_ITEM_KEY + ":" + itemId + ":base";
        //商品信息在redis中的过期时间，(60*60*24）
        final int timeExpire = 86400;
        try {
            String json = jedisClient.get(redisKey);
            if (!StringUtils.isEmpty(json)) {
                Item item = JSON.parseObject(json, Item.class);
                return item;
            }
        } catch (Exception e) {
            log.error("Select Item Base Info To Redis Fails ------- itemId = " + itemId);
        }

        TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);
        Item item = new Item(tbItem);

        try {
            //将商品存入缓存中
            jedisClient.set(redisKey, JSON.toJSONString(item));
            //设置key的有效期
            jedisClient.expire(redisKey, timeExpire);
        } catch (Exception e) {
            log.error("Insert Item Base Info To Redis Fails ------ itemId = " + itemId);
        }
        return item;
    }

    @Override
    public String getItemDesc(Long itemId) {
        TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
        return JSON.toJSONString(itemDesc);
    }

    @Override
    public String getItemParam(Long itemId) {//根据商品id查询规格参数
        //设置查询条件
        TbItemParamItemExample example = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        //执行查询
        List<TbItemParamItem> list = itemParamItemMapper.selectByExampleWithBLOBs(example);
        if (list != null && list.size()>0) {
            TbItemParamItem paramItem = list.get(0);
            return JSON.toJSONString(DefaultResult.ok(paramItem));
        }
        return JSON.toJSONString(DefaultResult.build(400, "无此商品规格"));
    }
}
